﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using SharingCenter.Web.Classes.Events;
using SharingCenter.Web.Classes;
using SharingCenter.Web.Classes.Requests;
using SharingCenter.Web.Classes.enums;

namespace SharingCenter.Web.Mod.Events
{
    /// <summary>
    /// Summary description for EventServices
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class EventServices : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod]
        public bool ApproveEventRequest(string requestId)
        {
            bool ret = false;
            if (Context.User.Identity.IsAuthenticated)
            {
                Guid rid = new Guid(requestId);
                try
                {
                    using (scDataContext dataContext = new scDataContext())
                    {
                        Sc_SharedRequest request = RequestOperations.GetRequestById(rid, dataContext);
                        RequestOperations.ChangeRequestStatus(rid, RequestStatus.Approved, dataContext, false);
                        Guid eventId = new Guid(request.Context.Element("EventId").Value);
                        EventOperations.AddParticipantToEvent(eventId, request.RequestorUserId, "", dataContext, true);
                        ret = true;
                    }
                }
                catch { }
            }

            return ret;
        }

        [WebMethod]
        [ScriptMethod]
        public bool DeleteEvent(string eventId)
        {
            using (scDataContext cxt = new scDataContext())
            {
                try
                {
                    Guid eid = new Guid(eventId);
                    Sc_Event evt = cxt.Sc_Events.Single(d => d.EventId == eid);
                    cxt.Sc_EventParticipants.DeleteAllOnSubmit(evt.Sc_EventParticipants);
                    cxt.Sc_Events.DeleteOnSubmit(evt);
                    cxt.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }
        }

        [WebMethod]
        [ScriptMethod]
        public bool? SetHot(string eventId)
        {
            using (scDataContext cxt = new scDataContext())
            {
                try
                {
                    Guid eid = new Guid(eventId);
                    Sc_Event evt = cxt.Sc_Events.Single(d => d.EventId == eid);
                    bool hot = evt.IsHot;
                    evt.IsHot = !hot;
                    cxt.SubmitChanges();
                    return !hot;
                }
                catch { return null; }
            }
        }

        [WebMethod]
        [ScriptMethod]
        public bool? SetRecommended(string eventId)
        {
            using (scDataContext cxt = new scDataContext())
            {
                try
                {
                    Guid eid = new Guid(eventId);
                    Sc_Event evt = cxt.Sc_Events.Single(d => d.EventId == eid);
                    bool rec = evt.IsRecommended;
                    evt.IsRecommended = !rec;
                    cxt.SubmitChanges();
                    return !rec;
                }
                catch { return null; }
            }
        }

        [WebMethod]
        [ScriptMethod]
        public bool SignUp(string eventId, string userId)
        {

            return true;
        }

        [WebMethod]
        [ScriptMethod]
        public bool CancelParticipateEvent(string eventId, string userId)
        {
            Guid eid, uid;
            try
            {
                eid = new Guid(eventId);
                uid = new Guid(userId);

            }
            catch
            {
                return false;
            }
            using (scDataContext cxt = new scDataContext())
            {
                try
                {
                    Sc_SharedRequest r = cxt.Sc_SharedRequests.Single(d => (d.RequestorUserId == uid && d.ContextId == eid));
                    //RequestOperations.ChangeRequestStatus(eid, uid, RequestStatus.Cancelled);
                    cxt.Sc_SharedRequests.DeleteOnSubmit(r);
                }
                catch { }

                try
                {
                    var q = from t in cxt.Sc_EventParticipants
                            where t.UserId == uid && t.EventId == eid
                            select t;

                    if (q.Count() > 0)
                    {
                        cxt.Sc_EventParticipants.DeleteAllOnSubmit(q);
                    }

                    

                }
                catch { return false; }

                cxt.SubmitChanges();
            }
                return true;
            
        }
    }
}
